package puyu.manage.service.file.controller;

import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Delete;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.multipart.MultipartFile;
import puyu.manage.service.common.config.BaseController;
import puyu.manage.service.constant.AjaxResult;
import puyu.manage.service.file.entity.SysTFileUpload;
import puyu.manage.service.file.entity.vo.FileUploadVo;
import puyu.manage.service.file.service.SysTFileCenterService;
import puyu.manage.service.file.service.SysTFileUploadService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

import static puyu.manage.service.constant.AjaxResult.success;


/**
 * 文件表(SysTFileUpload)表控制层
 *
 * @author makejava
 * @since 2024-06-07 15:12:51
 */
@Slf4j
@Api(tags = "文件表")
@RestController
@RequestMapping("/sysTFileUpload")
public class SysTFileUploadController extends BaseController {

    /**
     * 服务对象
     */
    @Resource
    private SysTFileUploadService sysTFileUploadService;

    @Resource
    private SysTFileCenterService sysTFileCenterService;

    /**
     * 单个上传
     */
    @ApiOperation("单个上传")
    @PostMapping("/upload")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult upload(@RequestBody MultipartFile file) {
        SysTFileUpload upload = sysTFileUploadService.upload(file);
        return success("上传成功",upload);
    }


    /**
     * 批量上传
     */
    @ApiOperation("批量上传")
    @PostMapping("/uploads")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult uploads(@RequestBody MultipartFile[] file) {
        sysTFileUploadService.uploads(file);
        return success("上传成功");
    }

    /**
     * 业务单据关联保存
     **/
    @ApiOperation("业务单据关联保存")
    @PostMapping("/saveByFile")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult saveByFile(@RequestBody FileUploadVo vo) {
        sysTFileCenterService.saveByFile(vo);
        return success("上传成功");
    }


    /**
     * 通过业务ID查询文件数据
     **/
    @ApiOperation("通过业务ID查询文件数据")
    @GetMapping("/getByFile")
    public AjaxResult getByFile(String id) {
        startPage();
        List<SysTFileUpload> byFile = sysTFileCenterService.getByFile(id);
        return success("查询成功",getDataTable(byFile));
    }

    /**
     * 删除文件数据
     * id 业务ID
     **/
    @ApiOperation("删除文件数据")
    @Delete("/deleteByFile")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult deleteByFiles(@Validated String id) {
        sysTFileCenterService.deleteByFiles(id);
        return success("删除成功");
    }

}

